package utils

import (
	"encoding/json"
	"errors"
	openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
	ocr_api20210707 "github.com/alibabacloud-go/ocr-api-20210707/client"
	util "github.com/alibabacloud-go/tea-utils/v2/service"
	"github.com/alibabacloud-go/tea/tea"
)

func createClient() (_result *ocr_api20210707.Client, _err error) {
	config := &openapi.Config{AccessKeyId: tea.String(ServerConf.OCR.AccessKeyId), AccessKeySecret: tea.String(ServerConf.OCR.AccessKeySecret)}
	// 访问的域名
	config.Endpoint = tea.String("ocr-api.cn-hangzhou.aliyuncs.com")
	_result = &ocr_api20210707.Client{}
	_result, _err = ocr_api20210707.NewClient(config)
	return _result, _err
}

func OCRFace(facePath string) (res map[string]string, err error) {
	// 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378661.html
	client, err := createClient()
	if err != nil {
		return nil, errors.New("解析身份证服务异常！")
	}

	recognizeIdcardRequest := &ocr_api20210707.RecognizeIdcardRequest{
		OutputFigure: tea.Bool(true),
		Url:          tea.String(facePath),
	}
	runtime := &util.RuntimeOptions{}
	resp, err := client.RecognizeIdcardWithOptions(recognizeIdcardRequest, runtime)
	if err != nil {
		return nil, errors.New("请上传标准的身份证头像面图片！")
	}

	var v interface{}
	json.Unmarshal([]byte(*resp.Body.Data), &v)
	i, ok := v.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证正面异常！")
	}
	data := i["data"]
	j, ok := data.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证正面异常！")
	}
	face := j["face"]
	k, ok := face.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证正面异常！")
	}
	backData := k["data"]
	h, ok := backData.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证正面异常！")
	}

	res = make(map[string]string)
	address := h["address"]
	res["address"] = address.(string)
	birthDate := h["birthDate"]
	res["birthDate"] = birthDate.(string)
	ethnicity := h["ethnicity"]
	res["ethnicity"] = ethnicity.(string)
	idNumber := h["idNumber"]
	res["idNumber"] = idNumber.(string)
	name := h["name"]
	res["name"] = name.(string)
	sex := h["sex"]
	res["sex"] = sex.(string)
	return res, nil
}

func OCRBack(facePath string) (res map[string]string, err error) {
	// 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378661.html
	client, err := createClient()
	if err != nil {
		return nil, errors.New("解析身份证服务异常！")
	}

	recognizeIdcardRequest := &ocr_api20210707.RecognizeIdcardRequest{
		OutputFigure: tea.Bool(true),
		Url:          tea.String(facePath),
	}
	runtime := &util.RuntimeOptions{}
	resp, err := client.RecognizeIdcardWithOptions(recognizeIdcardRequest, runtime)
	if err != nil {
		return nil, errors.New("请上传标准的身份证国徽面图片！")
	}

	var v interface{}
	json.Unmarshal([]byte(*resp.Body.Data), &v)
	i, ok := v.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证背面异常！")
	}
	data := i["data"]
	j, ok := data.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证背面异常！")
	}
	back := j["back"]
	k, ok := back.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证背面异常！")
	}
	backData := k["data"]
	h, ok := backData.(map[string]interface{})
	if !ok {
		return nil, errors.New("解析身份证背面异常！")
	}

	res = make(map[string]string)
	issueAuthority := h["issueAuthority"]
	res["issueAuthority"] = issueAuthority.(string)
	validPeriod := h["validPeriod"]
	res["validPeriod"] = validPeriod.(string)
	return res, nil
}
